package com.atguigu.web.biz;

import com.atguigu.web.bean.Person;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.function.ServerRequest;
import org.springframework.web.servlet.function.ServerResponse;

import java.util.Arrays;
import java.util.List;

/**
 * @author DongZH
 * @content 专门处理User有关的业务
 * @since 2024/4/1 - 21:18
 **/
@Service
@Slf4j
public class UserBizHandler {

    /**
     * 查询指定id的用户
     * @param request
     * @return
     * @throws Exception
     */
    public ServerResponse getUser(ServerRequest request) throws Exception {
        String id = request.pathVariable("id");
        log.info("查询【{}】用户信息", id);
        // 业务处理
        Person person = new Person(1L, "哈哈", "aa@qq.com", 18, "admin");
        // 构造响应
        return ServerResponse.ok().body(person);
    }

    /**
     * 获取所有用户
     * @param request
     * @return
     * @throws Exception
     */
    public ServerResponse getUsers(ServerRequest request) throws Exception {
        log.info("查询所有用户信息");
        // 业务处理
        List<Person> list = Arrays.asList(new Person(1L, "哈哈1", "aa1@qq.com", 18, "admin1"),
                new Person(2L, "哈哈2", "aa2@qq.com", 19, "admin2"),
                new Person(3L, "哈哈3", "aa3@qq.com", 20, "admin3"),
                new Person(4L, "哈哈4", "aa4@qq.com", 21, "admin4"),
                new Person(5L, "哈哈5", "aa5@qq.com", 22, "admin5"));
        // 构造响应
        return ServerResponse.ok().body(list);  // 凡是body中的对象，就是以前@ResponseBody原理。利用HttpMessageConverter写出json
    }

    /**
     * 保存用户
     * @param request
     * @return
     * @throws Exception
     */
    public ServerResponse saveUser(ServerRequest request) throws Exception {
        // 提取请求体
        Person body = request.body(Person.class);
        // 业务处理
        log.info("保存用户信息{}", body);
        // 构造响应
        return ServerResponse.ok().build();
    }

    /**
     * 更新用户信息
     * @param request
     * @return
     * @throws Exception
     */
    public ServerResponse updateUser(ServerRequest request) throws Exception {
        log.info("修改用户信息成功");
        return ServerResponse.ok().build();
    }

    /**
     * 删除新用户信息
     * @param request
     * @return
     * @throws Exception
     */
    public ServerResponse deleteUser(ServerRequest request) throws Exception {
        log.info("删除用户信息成功");
        return ServerResponse.ok().build();
    }
}
